package com.cdtye.itps.cms.service.equipment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdtye.common.mybatiscore.base.BaseService;
import com.cdtye.itps.cms.dao.equipment.entity.EquipmentClassType;
import com.cdtye.itps.cms.dao.equipment.entity.EquipmentDivceAttr;
import com.cdtye.itps.cms.dao.equipment.vo.EquipmentClassTypeVo;
import com.cdtye.itps.cms.dao.equipment.vo.EquipmentDivceAttrVo;
import com.cdtye.itps.cms.mapper.equipment.EquipmentClassTypeMapper;
import com.cdtye.itps.cms.mapper.equipment.EquipmentDivceAttrMapper;
import com.cdtye.itps.cms.service.dept.SysDeptService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class EquipmentDivceAttrService extends BaseService<EquipmentDivceAttrMapper, EquipmentDivceAttr> {

    @Autowired
    private EquipmentDivceAttrMapper modelMapper;

    @Autowired
    private EquipmentClassTypeMapper equipmentClassTypeMapper;

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 列表查询
     * @param vo
     * @return
     */
    public List<EquipmentDivceAttr> list(EquipmentDivceAttrVo vo){
        LambdaQueryWrapper<EquipmentDivceAttr> wrapper = new LambdaQueryWrapper<>();
        //添加查询条件

        return list(wrapper);
    }

    /**
     * 分页查询，自定义sql语句
     * @param vo
     * @return
     */
    public IPage<Map<String,Object>> listPage(EquipmentDivceAttrVo vo){
//        if (vo.getDeptId() == null || vo.getDeptId() == "" ){
//            UserSso loginUser = AuthHelper.loginUser();
//            String unitDeptCode = loginUser.getUnitDeptCode(); //获取当前登入人的单位标识
//            String userName = loginUser.getUsername();
//            Integer gzjAccount = loginUser.getGzjAccount();   //判断是不是局级账户
//            List<String> unitDeptTypeList = loginUser.getUnitDeptTypeList();
//            if(gzjAccount!=null && gzjAccount == 1){
//                unitDeptCode = null;
//            }
//            if(!CollectionUtils.isEmpty(unitDeptTypeList)&&unitDeptTypeList.contains(SysDeptTypeEnum.JU_GDC.getDeptType())){
//                unitDeptCode = null;
//            }
//            //如果是超级管理员进来，那么也是获取所有单位
//            if(AuthConstants.ADMIN_ACCOUNT.equals(userName)){
//                unitDeptCode = null;
//            }
//            if ("01".equals(unitDeptCode)) { //朔黄总公司特殊处理
//                unitDeptCode = null;
//            }
//            vo.setDeptId(unitDeptCode);
//        }
        vo.setDeptId(sysDeptService.getUnitDeptCodeByLoginUser(vo.getDeptId()));
        return modelMapper.listByPage(vo.page(), vo);
    }


    /**
     * 统计 总数 崭新 JCJC001      良好 JCJC002  老化 JCJC003   报废JCJC004
     *
     */
    public Map<String,String> count(EquipmentDivceAttrVo vo) throws Exception{
        HashMap<String, String> map = new HashMap<>();
        map.put("totle","0"); //总数
        map.put("news","0");//崭新
        map.put("good","0");//良好
        map.put("old","0");//老化
        map.put("waste","0");//报废
        String typeNo = vo.getTypeNo();
        typeNo=typeNo+"%";
        vo.setTypeNo(typeNo);
        List<Map<String, Object>> count = modelMapper.count(vo);
        if(CollectionUtils.isEmpty(count)){
            return map;
        }
        for (Map m:count) {
            String num =String.valueOf( m.get("num"));
            String divceState = (String) m.get("divceState");
            if(divceState.equals("JCJC001")){
                map.put("news", num);//崭新
            }
            if(divceState.equals("JCJC002")){
                map.put("good",num);//崭新
            }
            if(divceState.equals("JCJC003")){
                map.put("old",num);//崭新
            }
            if(divceState.equals("JCJC004")){
                map.put("waste",num);//崭新
            }
        }
        map.put("totle",String.valueOf(map.size()));


        return map;
    }

    /**
     * 获取状态信息集合
     * @return
     */
    public List<Map<String, Object>> getStates() {
        return modelMapper.getDivceStatsDtId("JCJCSTATE");
    }

    public List<Map<String, Object>> getTypeList(EquipmentDivceAttrVo vo) {
        String typeNo = vo.getTypeNo();
        typeNo=typeNo+"%";
        EquipmentClassTypeVo equipmentClassTypeVo = new EquipmentClassTypeVo();
        equipmentClassTypeVo.setTypeNo(typeNo);
        List<Map<String, Object>> redundant = equipmentClassTypeMapper.isRedundant(equipmentClassTypeVo);
        return redundant;
    }

    /**
     * 编码生成
     * @param model
     * @return
     */
    public EquipmentDivceAttr saveOrUpdates(EquipmentDivceAttr model) {
        EquipmentClassType equipmentClassType = equipmentClassTypeMapper.selectById(model.getTypeId());
        String typeNo = equipmentClassType.getTypeNo();
        List<Map<String, Object>> list = modelMapper.getAll();

        Integer integer=0;
        if(list.isEmpty()){
            integer=0;
        }else {
            //后六位
            Map<String, Object> map = list.get(0);
            String eqtNumber = String.valueOf(map.get("eqtNumber"));
            String substring = eqtNumber.substring(eqtNumber.length() - 6, eqtNumber.length());
            integer= Integer.valueOf(substring);
        }
        String  newStr = String.valueOf(integer + 1);
        if (newStr.length() < 6) {
            while (newStr.length() < 6) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(newStr);// 左补0
                // sb.append(str).append("0");//右补0
                newStr = sb.toString();
            }
        }
        typeNo=typeNo+newStr;
        model.setEqtNumber(typeNo);
        return model;
    }

}
